import services

import pojo
import os

class FileCrawler:
    def __init__(self, debug = False):
        self.debug = debug
        self.data_store_service = services.resolve_service("datastoreservice")
        f = pojo.FilePath("","")
        self.data_store_service.initialize(f)
        self.data_store_service.createschema(f)

    def crawl(self, dir_to_crawl):
        if self.debug:
            print __file__, " crawl called with ", dir_to_crawl

        if os.path.isfile(dir_to_crawl):
            return 

        for r, d, f in os.walk(dir_to_crawl, topdown = True):
            if self.debug:
                print "{0} is seed".format(r)

            for dd in d:
                self.post(r, os.path.join(r, dd))

            for ff in f:
                self.post(r, os.path.join(r, ff))

            del d[:]

    def post(self, parent_path,  child_path):
        if self.debug:
            print "about to post parent ", parent_path, " child ", child_path
        f = pojo.FilePath(parent_path, child_path)
        self.data_store_service.save(f)
        
